package models

import (
	"time"
)

// News 新闻模型
type News struct {
	ID        uint      `json:"id" gorm:"primaryKey;autoIncrement"`
	Title     string    `json:"title" gorm:"type:varchar(255);not null"`
	Content   string    `json:"content" gorm:"type:text;not null"`
	Author    string    `json:"author" gorm:"type:varchar(100);not null"`
	CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}

// TableName 指定表名
func (News) TableName() string {
	return "news"
}

// CreateNewsRequest 创建新闻请求
type CreateNewsRequest struct {
	Title   string `json:"title" binding:"required,max=255"`
	Content string `json:"content" binding:"required"`
	Author  string `json:"author" binding:"required,max=100"`
}

// UpdateNewsRequest 更新新闻请求
type UpdateNewsRequest struct {
	Title   string `json:"title" binding:"required,max=255"`
	Content string `json:"content" binding:"required"`
	Author  string `json:"author" binding:"required,max=100"`
}

// NewsResponse 新闻响应
type NewsResponse struct {
	ID        uint      `json:"id"`
	Title     string    `json:"title"`
	Content   string    `json:"content"`
	Author    string    `json:"author"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

// ToResponse 转换为响应格式
func (n *News) ToResponse() NewsResponse {
	return NewsResponse{
		ID:        n.ID,
		Title:     n.Title,
		Content:   n.Content,
		Author:    n.Author,
		CreatedAt: n.CreatedAt,
		UpdatedAt: n.UpdatedAt,
	}
}